package com.lenovo.leos.filebrowser.engine.operation;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


public class FileOp extends Object {
	private static final String TAG = "FileOp";
	
	// request code
	public static final int FOP_CREATE_DIR = 10001;
	public static final int FOP_CREATE_FILE = 10002;
	public static final int FOP_DELETE = 10003;
	public static final int FOP_RENAME = 10004;
	public static final int FOP_COPY = 10005;
	public static final int FOP_CUT = 10006;
	public static final int FOP_SHARE = 10007;
	public static final int FOP_COMPRESS = 10008;
	public static final int FOP_DECOMPRESS = 10009;
	public static final int FOP_PROPERTY = 10010;

    public static final int SRC_FROM_BUNDLE = 1;
    public static final int SRC_FROM_CLIP = 2;
	
//	public static final int FOP_LAST = 10;
	
	// bundle keys
    public static final String KEY_INT_FOP_CODE = "request_code";
    
	public static final String KEY_STRING_SRC_DIR_PATH = "src_dir_path";
	public static final String KEY_STRING_SRC_FILE_PATH = "src_file_path";
	public static final String KEY_SERIAL_SRC_FILES = "src_files";
	public static final String KEY_STRING_SRC_FILE_NAME = "src_file_name";
	
	public static final String KEY_STRING_DEST_FILE_NAME = "dest_file_name";
	public static final String KEY_STRING_DEST_DIR_PATH = "dest_dir_path";

    public static final String KEY_INT_SRC_FROM = "src_from";
	
	public static final String KEY_BOOLEAN_IS_ZIP = "is_zip";	
	
	/* bundle data keys for various operations
	 * create new folder:		DEST_DIR_PATH		DEST_FILE_NAME		
	 * create new file:			DEST_DIR_PATH		DEST_FILE_NAME
	 * copy files:				SRC_FILES 			DEST_DIR_PATH 
	 * cut files:				SRC_FILES  			DEST_DIR_PATH 
	 * rename:					SRC_DIR_PATH 		SRC_FILE_NAME		DEST_FILE_NAME
	 * decompress:				DEST_DIR_PATH		IS_ZIP				SRC_FILE_PATH
	 * compress:				SRC_FILES			DEST_DIR_PATH		DEST_FILE_NAME
	 * delete:					SRC_FILES
	 *                          SRC_FILE_PATH
	 * 
	 */
	
	private static boolean checkBundleForNew(Bundle bundle){
		if (null == bundle)
			return false;
		
		if (null == bundle.getString(KEY_STRING_DEST_DIR_PATH) ||
				null == bundle.getString(KEY_STRING_DEST_FILE_NAME)){
			Log.e(TAG, "no need data in bundle for create new dir/file");
			return false;
		}else 
			return true;
	}
	
	private static boolean checkBundleForCopyOrCut(Bundle bundle){
		Log.i(TAG, "== >> copy file ");
		if (null == bundle)
			return false;
		
		if (null == bundle.getString(KEY_STRING_DEST_DIR_PATH)){
			Log.e(TAG, "no need data in bundle for copy/cut");
			return false;
		}else 
			return true;
	}
	
	private static boolean checkBundleForRename(Bundle bundle){
		if (null == bundle)
			return false;
		
		if (null == bundle.getString(KEY_STRING_SRC_DIR_PATH) ||
				null == bundle.getString(KEY_STRING_SRC_FILE_NAME) ||
				null == bundle.getString(KEY_STRING_DEST_FILE_NAME)){
			Log.e(TAG, "no need data in bundle for rename");
			return false;
		}else 
			return true;		
	}
	
	private static boolean checkBundleForDecompress(Bundle bundle) {
		if (null == bundle)
			return false;
		
		if (null == bundle.getString(KEY_STRING_DEST_DIR_PATH) ||
				null == bundle.getString(KEY_STRING_SRC_FILE_PATH)){
			Log.e(TAG, "no need data in bundle for decompress");
			return false;
		}else 
			return true;		
	}
	
	private static boolean checkBundleForCompress(Bundle bundle) {
		Log.i(TAG, "==> compress file");
		if (null == bundle)
			return false;
		
		int srcFrom = bundle.getInt(KEY_INT_SRC_FROM);
		if (0 == srcFrom) {
			Log.e(TAG, "invalid data in bundle for delete");
			return false;
		} else {
			if (SRC_FROM_BUNDLE == srcFrom) {
				if (null == bundle.getString(KEY_STRING_SRC_FILE_PATH)) {
					Log.e(TAG, "no need data in bundle for delete");
					return false;
				}
			} else if (SRC_FROM_CLIP == srcFrom) {

			} else {
				Log.e(TAG, "unknown data in bundle for delete");
				return false;
			}
			
			if (null == bundle.getString(KEY_STRING_DEST_FILE_NAME)) {
				return false;
			}
			
			if (null == bundle.getString(KEY_STRING_DEST_DIR_PATH)) {
				return false;
			}
			
			return true;
		}		
	}
	
	private static boolean checkBundleForDelete(Bundle bundle) {
		Log.i(TAG, "==> delete file");
        if (null == bundle)
                return false;
        
		int srcFrom = bundle.getInt(KEY_INT_SRC_FROM);
		if (0 == srcFrom) {
			Log.e(TAG, "invalid data in bundle for delete");
			return false;
		} else {
			if (SRC_FROM_BUNDLE == srcFrom) {
				if (null == bundle.getString(KEY_STRING_SRC_FILE_PATH)) {
					Log.e(TAG, "no need data in bundle for delete");
					return false;
				} else
					return true;
			} else if (SRC_FROM_CLIP == srcFrom) {
				return true;
			} else {
				Log.e(TAG, "unknown data in bundle for delete");
				return false;
			}
		}
	}
	
	private static void launchActivity(Activity activity, Bundle bundle, int opCode) {
		Log.i(TAG, "==> launchActivity");
		Intent intent = new Intent();
		
		if (FOP_COMPRESS == opCode || FOP_DECOMPRESS == opCode){
			intent.setClass(activity, FileCompressor.class);
		}else{
			intent.setClass(activity, FileMan.class);
		}
		intent.putExtras(bundle);
		activity.startActivityForResult(intent, opCode);		
	}
	
	public static boolean launchActivityToDo(Activity activity, Bundle bundle, int opCode){
		Log.i(TAG, "==> launchActivityToDo");
		
		if (null == activity){
			Log.e(TAG, "acitivity is null in callFileManToDo");
			return false;
		}
		
		if (0 > opCode || FOP_PROPERTY < opCode){
			Log.e(TAG, "invalid operation code in callFileManToDo");
			return false;
		}
		
		boolean isValidBundle = false;
		
		switch(opCode){
		case FOP_CREATE_DIR:
		case FOP_CREATE_FILE:
			isValidBundle = checkBundleForNew(bundle);
			break;
		case FOP_CUT:
		case FOP_COPY:
			isValidBundle = checkBundleForCopyOrCut(bundle);
			break;
		case FOP_DELETE:
			isValidBundle = checkBundleForDelete(bundle);
			break;
		case FOP_RENAME:
			isValidBundle = checkBundleForRename(bundle);
			break;
		case FOP_COMPRESS:
			isValidBundle = checkBundleForCompress(bundle);
			break;
		case FOP_DECOMPRESS:
			isValidBundle = checkBundleForDecompress(bundle);
			break;
		case FOP_PROPERTY:
			Log.e(TAG, "not support to get properties from activity now");
			return false;
		default:
			Log.e(TAG, "invalid operation code");
			return false;
		}
		
		if (isValidBundle){
			launchActivity(activity, bundle, opCode);
			return true;
		}else
			return false;
	}	
}
